package com.choquero.audiotouch.fragments;

import com.choquero.audiotouch.R;
import com.choquero.audiotouch.model.Song;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment_SongList extends ListFragment {

	private Cursor cursor;
	private int selected;
	private static MediaPlayer mp = new MediaPlayer();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		String[] proj = { MediaStore.Audio.Media.TITLE,
				MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.ARTIST,
				MediaStore.Audio.Media.DURATION,
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.ALBUM_ID
				};
		cursor = getActivity().getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null,
				MediaStore.Audio.Media.TITLE);

		setListAdapter(new SongAdapter(getActivity(), cursor));
		
		mp.reset();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		cursor.moveToPosition(position);
		if(mp.isPlaying()){
			mp.stop();
			mp.reset();
		}
		
		if( position != this.selected ){
			try {
				mp.setDataSource(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
				mp.prepare();
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			mp.start();
		}
		
		
		
		
		
	}

	private class SongAdapter extends ArrayAdapter<Song> {

		private Activity activity;
		private Cursor cursor;
		private LayoutInflater inflater;

		public SongAdapter(Context context, Cursor cursor) {
			super(context, R.layout.fragment_songlist_row);

			this.activity = (Activity) context;
			this.cursor = cursor;
			this.inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return cursor.getCount();
		}

		@Override
		public Song getItem(int position) {
			cursor.moveToPosition(position);
			Song song = new Song(
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)),
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)),
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION))

			);

			return song;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View vi = convertView;
			if (convertView == null)
				vi = inflater.inflate(R.layout.fragment_songlist_row, null);

			TextView title = (TextView) vi.findViewById(R.id.title); // title
			TextView artist = (TextView) vi.findViewById(R.id.artist); // artist
																		// name
			TextView duration = (TextView) vi.findViewById(R.id.duration); // duration
			ImageView thumb_image =(ImageView)vi.findViewById(R.id.list_image); // thumb
			// image

			cursor.moveToPosition(position);

			title.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
			artist.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
			duration.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
			
			Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
//			Uri sArtworkUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
			Long album_id=cursor.getLong(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));	
			Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
			
			thumb_image.setImageURI(uri);
//			thumb_image.setImageResource(R.drawable.rihanna);

			return vi;
		}

	}
}
